// pages/resetpwd/resetpwd.js
const http = require("../../utils/http")
const util = require("../../utils/util")
const api = require("../../comm/api")

import Notify from "../../components/@vant/weapp/notify/notify"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        error_oldPassword: '',
        error_newPassword: '',
        error_newPassword2: '',
        isLoading: false
    },

    onSubmit: function (e) {
        const params = e.detail.value;
        const oldPassword_valied = !!params.oldPassword
        const newPassword_valied = !!params.newPassword
        const newPassword2_valied = !!params.newPassword2 && (params.newPassword == params.newPassword2)
        this.setData({
            error_oldPassword: oldPassword_valied ? "" : "原密码不能为空",
            error_newPassword: newPassword_valied ? "" : "新密码不能为空",
            error_newPassword2: newPassword2_valied ? "" : "两次密码不一致",
        })
        if (oldPassword_valied && newPassword_valied && newPassword2_valied) {
            this.setData({
                isLoading: true
            })
            delete params['newPassword2']
            http.postJSONWithToken(api.User.ChangePsw.PATH, params, pojo => {
                Notify({
                    type: 'success',
                    message: '修改成功',
                    onClose: () => {
                        util.clean()
                        wx.reLaunch({
                            url: '/pages/login/login',
                        })
                    }
                })
            }, err => {
                Notify({
                    type: 'warning',
                    message: JSON.stringify(err)
                })
                this.setData({
                    isLoading: false
                })
            })
        }
    }
})